package com.zprmb.udzt;

import java.util.ArrayList;

import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnLastItemVisibleListener;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.zprmb.udzt.adapter.ALLJiluAdapter;
import com.zprmb.udzt.bean.JiLu_Bean;
import com.zprmb.udzt.bean.V4InfoBean;
import com.zprmb.udzt.http.AjaxParams;
import com.zprmb.udzt.util.Constant;
import com.zprmb.udzt.util.VolleryUtil;
/**
 * 忘记密码
 * @author shenshao
 *
 */
public class ProductDetailsJiluActivity extends BaseActivity implements OnClickListener {

	private V4InfoBean info;
	private String id;
	private int page = 2;
	private PullToRefreshListView lv_all_jilu;
	private ListView actualListView;
	private ImageView iv_all_null;
	private boolean isyou = true;
	private TextView invest_num,invest_count;
	private ArrayList<JiLu_Bean> dateOfadapter = new ArrayList<JiLu_Bean>();
	private ALLJiluAdapter adapter;
	private ImageView iv_v4_productdetails_jilu_back;
	private TextView tv_v4_productdetails_jilu_title;
	private RelativeLayout rl_back,rl_invest_info;
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		BbtApplication.getInstance().removeActvity(this);
	}
	
	Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case 1:// 正常
				if (info.jb.size() < 1) {
					lv_all_jilu.setVisibility(View.GONE);
					iv_all_null.setVisibility(View.VISIBLE);
				} else {
					iv_all_null.setVisibility(View.GONE);
					lv_all_jilu.setVisibility(View.VISIBLE);
					dateOfadapter.clear();
					dateOfadapter.addAll(info.jb);
					adapter = new ALLJiluAdapter(ProductDetailsJiluActivity.this,
							dateOfadapter);
					actualListView.setAdapter(adapter);
					lv_all_jilu.onRefreshComplete();
				}
				break;
			case 2:// 下拉
				isyou = true;
				dateOfadapter.clear();
				dateOfadapter.addAll(info.jb);
				adapter = new ALLJiluAdapter(ProductDetailsJiluActivity.this,
						dateOfadapter);
				actualListView.setAdapter(adapter);
				lv_all_jilu.onRefreshComplete();
				break;
			case 3:// 上拉
				if (info.jb.size() < 1) {
					isyou = false;
				}
				dateOfadapter.addAll(info.jb);
				adapter.notifyDataSetChanged();
				lv_all_jilu.onRefreshComplete();
				break;
			}
		};
	};
	private String who;
	private boolean flagFresh;
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.v4_product_details_jilu_activity);
		 BbtApplication.getInstance().addActvity(this);
		Intent intent = getIntent();
		id = intent.getStringExtra("id");
		who = intent.getStringExtra("who");
		flagFresh = intent.getBooleanExtra("flagFresh", false);
		info = new V4InfoBean();
		initView();
		initData(1,1,true);
	}

	private void initView() {
		lv_all_jilu = (PullToRefreshListView)findViewById(R.id.lv_all_jilu);
		actualListView = lv_all_jilu.getRefreshableView();
		registerForContextMenu(actualListView);
		iv_all_null = (ImageView) findViewById(R.id.iv_all_null);
		iv_v4_productdetails_jilu_back = (ImageView) findViewById(R.id.img_back);
		rl_back=(RelativeLayout)findViewById(R.id.rl_back);
		rl_invest_info=(RelativeLayout)findViewById(R.id.rl_invest_info);
		invest_num=(TextView)findViewById(R.id.invest_num);
		invest_count=(TextView)findViewById(R.id.invest_count);
		rl_back.setOnClickListener(this);
		
		iv_v4_productdetails_jilu_back.setOnClickListener(this);
		tv_v4_productdetails_jilu_title = (TextView) findViewById(R.id.tv_v4_productdetails_jilu_title);
		tv_v4_productdetails_jilu_title.getPaint().setFakeBoldText(true);
		tv_v4_productdetails_jilu_title.setText("投标记录");
		lv_all_jilu.setOnRefreshListener(new OnRefreshListener<ListView>() {
			public void onRefresh(PullToRefreshBase<ListView> refreshView) {
				page = 2;
				initData(1, 2, false);
			}
		});
		lv_all_jilu.setOnLastItemVisibleListener(new OnLastItemVisibleListener() {
			public void onLastItemVisible() {
				if (isyou) {
					initData(page, 3, false);
					page++;
				}
			}
		});
	}

	private void initData(int page, final int handlerId, boolean isShow) {
		AjaxParams params = new AjaxParams();
		params.put("pageNo", page + "");
		params.put("pageSize", "12");
		params.put("flagFresh", flagFresh+"");
		if (!who.equals("uplan")) {
			params.put("loanId", id);
		}
		VolleryUtil vollery = new VolleryUtil() {
			
			@Override
			public void onUpDate(JSONObject jsonObject) {
				// TODO Auto-generated method stub
				Log.e("投资记录", jsonObject.toString());
				if (jsonObject.optInt("success") == 1) {
					JSONObject json = jsonObject.optJSONObject("data");
					if (who.equals("uplan")) {
						JSONObject json2 = json.optJSONObject("pageInfo");
						JSONObject json3 = json.optJSONObject("loan");
						rl_invest_info.setVisibility(View.VISIBLE);
						invest_count.setText(json3.optString("investCount"));
						invest_num.setText(json3.optString("userCount"));
						info.parseJSON_jilu(json2.optJSONArray("resultList"),"uplan");
					}else {
						rl_invest_info.setVisibility(View.GONE);
						info.parseJSON_jilu(jsonObject.optJSONArray("data"),"product");//直投
					}
					if(getIntent().getIntExtra("Dexter", 0)==1){
						rl_invest_info.setVisibility(View.GONE);
					}
					handler.sendEmptyMessage(handlerId);
				}
			}
			
			@Override
			public void onError() {
				// TODO Auto-generated method stub
				
			}
		};
		vollery.setRequestMethod(0);
		vollery.setShow(isShow);
		if (who.equals("uplan")) {
			vollery.execute(this,Constant.URL + "api/v6/invest/detailInvestList/"+id+"/"+page+"/12","");
		}else {
			vollery.execute(this,Constant.URL + "apiLoan/investRecordPerLoan?",params.toString());
		}
	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.img_back:
			finish();
			break;
		case R.id.rl_back:
			finish();
			break;
		}
	}
}
